package com.tvunetworks.center.device.config.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
/**
 * @author lebron
 * @ClassName: MyMybatisProperties
 * @Description: Mybatis参数

 */
@Configuration
public class RedisConfiguration {
    /**
     *
     * @param hostName hostName
     * @param port port
     * @param password password
     * @param testOnBorrow testOnBorrow
     * @param maxIdle maxIdle
     * @param maxTotal maxTotal
     * @param index index
     * @param maxWaitMillis maxWaitMillis
     * @return StringRedisTemplate
     */
    @Bean(name = "redisDevTemplate")
    public StringRedisTemplate redisTemplate(@Value("${redis.slave.host}") String hostName,
                                             @Value("${redis.slave.port}") int port,
                                             @Value("${redis.slave.password}") String password,
                                             @Value("false") boolean testOnBorrow,
                                             @Value("10") int maxIdle,
                                             @Value("8") int maxTotal,
                                             @Value("0") int index,
                                             @Value("5000") long maxWaitMillis) {
        StringRedisTemplate temple = new StringRedisTemplate();
        temple.setConnectionFactory(
                connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow));

        return temple;
    }

    /**
     * tpds的redis
     * @param hostName redis地址host
     * @param port  redis端口
     * @param password redis密码
     * @param testOnBorrow testOnBorrow
     * @param maxIdle maxIdle
     * @param maxTotal maxTotal
     * @param index index
     * @param maxWaitMillis maxWaitMillis
     * @return tpds的reids连接
     */
    @Bean(name = "redisTpdsTemplate")
    public  RedisTemplate<Object, Object> redisTpdsTemplate(@Value("${redis.tpds.host:10.12.22.17}") String hostName,
                                             @Value("${redis.tpds.port:6379}") int port,
                                             @Value("${redis.tpds.password:}") String password,
                                             @Value("false") boolean testOnBorrow,
                                             @Value("${redis.tpds.maxIdle:10}") int maxIdle,
                                             @Value("8") int maxTotal,
                                             @Value("0") int index,
                                             @Value("${redis.tpds.maxWait:2000}") long maxWaitMillis) {
        RedisTemplate<Object, Object> temple = new RedisTemplate<Object, Object>();
        temple.setConnectionFactory(
                connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow));
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        temple.setValueSerializer(jackson2JsonRedisSerializer); //1
        temple.setKeySerializer(new StringRedisSerializer()); //2

        temple.afterPropertiesSet();
        return temple;
    }
//    @Bean(name = "redisPruduceTemplate")
//    public StringRedisTemplate redisTemplates(
//            @Value("10.12.22.100") String hostName,
//            @Value("6379") int port,
//            @Value("") String password,
//            @Value("false") boolean testOnBorrow,
//            @Value("2") int maxIdle,
//            @Value("3") int maxTotal,
//            @Value("0") int index,
//            @Value("5000") long maxWaitMillis) {
//        StringRedisTemplate temple = new StringRedisTemplate();
//        temple.setConnectionFactory(
//                connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow));
//
//        return temple;
//    }

    /**
     *
     * @param hostName hostName
     * @param port port
     * @param password password
     * @param maxIdle maxIdle
     * @param maxTotal maxTotal
     * @param index index
     * @param maxWaitMillis maxWaitMillis
     * @param testOnBorrow testOnBorrow
     * @return RedisConnectionFactory
     */
    public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle,
                                                    int maxTotal, int index, long maxWaitMillis, boolean testOnBorrow) {
        JedisConnectionFactory jedis = new JedisConnectionFactory();
        jedis.setHostName(hostName);
        jedis.setPort(port);
        if (StringUtils.isNotEmpty(password)) {
            jedis.setPassword(password);
        }
        if (index != 0) {
            jedis.setDatabase(index);
        }
        jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis, testOnBorrow));
        // 初始化连接pool
        jedis.afterPropertiesSet();
        RedisConnectionFactory factory = jedis;

        return factory;
    }

    /**
     *
     * @param maxIdle maxIdle
     * @param maxTotal maxTotal
     * @param maxWaitMillis maxWaitMillis
     * @param testOnBorrow testOnBorrow
     * @return JedisPoolConfig
     */
    public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
        JedisPoolConfig poolCofig = new JedisPoolConfig();
        poolCofig.setMaxIdle(maxIdle);
        poolCofig.setMaxTotal(maxTotal);
        poolCofig.setMaxWaitMillis(maxWaitMillis);
        poolCofig.setTestOnBorrow(testOnBorrow);
        return poolCofig;
    }
}

